//
//  RegisterComtroller2.m
//  DYLYJ
//
//  Created by JM on 15/11/21.
//  Copyright © 2015年 JM. All rights reserved.
//

#import "RegisterComtroller2.h"
#import "RegisterSecurityCodeVC2.h"
#import "DYUIKit.h"
#import "DYRequestAll.h"
#import "NSString-Helper.h"
#import "AppDelegate.h"
@interface RegisterComtroller2 ()
@property(nonatomic,strong)IBOutlet UITextField *nameTextFeild;
@property(nonatomic,strong)IBOutlet UITextField *pwdTextFeild;
@property(nonatomic,strong)IBOutlet UITextField *pwdQueryTextFeild;
@end

@implementation RegisterComtroller2

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)clickResigerBtn:(id)sender {
    [self.view endEditing:YES];
    
    if (self.nameTextFeild.text.length == 0){
        [MJAlertView showMessage:@"请输入用户名"];
        return;
    }
    if (self.pwdTextFeild.text.length == 0){
        [MJAlertView showMessage:@"请输入密码"];
        return;
    }
    if (self.pwdQueryTextFeild.text.length == 0){
        [MJAlertView showMessage:@"请输入确认密码"];
        return;
    }
    
    if (![self.pwdTextFeild.text isEqualToString:self.pwdQueryTextFeild.text]){
        [MJAlertView showMessage:@"密码不一致"];
        return;
    }
    
    if ([NSString isPhoneNumber:self.nameTextFeild.text]) {//手机
        UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        RegisterSecurityCodeVC2* vc = [storyboard instantiateViewControllerWithIdentifier:@"RegisterSecurityCodeVC2"];
        vc.name = self.nameTextFeild.text;
        vc.pwd = self.pwdTextFeild.text;
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([NSString isValidateEmail:self.nameTextFeild.text]){
        RegitsterEmailRequest *request = [RegitsterEmailRequest request];
        request.DY_userName = _nameTextFeild.text;
        request.DY_password = _pwdTextFeild.text;
        [request requstWithCompletionHandler:^(DYResponse *rs, NSError *error) {
            if (error){
                [MJAlertView showMessage:[error dy_description]];
                return ;
            }
            RegitsterEmailResponse *response = (RegitsterEmailResponse *)rs;
            myAppDelegate.uid = response.DY_uid;
            myAppDelegate.token = response.DY_token;
            myAppDelegate.islogin = YES;
            myAppDelegate.isDemo = NO;
            [DYSettingTool setObject:request.DY_userName forKey:DYLoginName];
            [DYSettingTool setObject:request.DY_password forKey:DYLoginPwd];
            [MJAlertView showAlertViewWithTitle:@"注册成功" message:@"系统将会发送一条验证码链接至你的新邮箱请登陆你的邮箱完成绑定步骤" clickedButtonBlock:^(NSInteger index) {
                [self.navigationController dismissViewControllerAnimated:YES completion:^{
                    
                }];
            } cancelButtonTitle:@"确定" otherButtonTitles:nil];
        }];
    } else {
        [MJAlertView showAlertViewWithTitle:@"提示" message:@"既不是手机也不是邮箱 确认要注册？" clickedButtonBlock:^(NSInteger index) {
            if (index == 0){//cancelButtonTitle 确定
                RegitsterNormalRequest *request = [RegitsterNormalRequest request];
                request.DY_userName = _nameTextFeild.text;
                request.DY_password = _pwdTextFeild.text;
                [request requstWithCompletionHandler:^(DYResponse *rs, NSError *error) {
                    if (error){
                        [MJAlertView showMessage:[error dy_description]];
                        return ;
                    }
                    RegitsterNormalResponse *response = (RegitsterNormalResponse *)rs;
                    myAppDelegate.uid = response.DY_uid;
                    myAppDelegate.token = response.DY_token;
                    myAppDelegate.islogin = YES;
                    myAppDelegate.isDemo = NO;
                    [DYSettingTool setObject:request.DY_userName forKey:DYLoginName];
                    [DYSettingTool setObject:request.DY_password forKey:DYLoginPwd];
                    [self.navigationController dismissViewControllerAnimated:YES completion:^{
                        
                    }];
                }];
            }
        } cancelButtonTitle:@"确定" otherButtonTitles:@"取消",nil];
        
        
    }
}

@end
